#!/bin/bash -e

function info() {
echo Usage: `basename $0` '[-e extend_bp] in.fa in.bed'
exit 2
}

while getopts  ":p:f:e:" opt; do
	case  $opt  in
		p) out_prefix=$OPTARG;;
		f) suffix=$OPTARG;;
		e) margin=$OPTARG;;
		*) info;;
	esac
done
shift $(($OPTIND - 1))


if [ $# -lt 1 ]; then info; fi

. $var

# IFS(Internal Field Seprator) IFS=$' \t\n'

margin=${margin:=100}

cat $2|awk '{if($2-"'$margin'" < 0){print $1"\t"0"\t"$3+"'$margin'"}else{print $1"\t"$2-"'$margin'""\t"$3+"'$margin'"}}' > margin.bed # awk $2 should no less 0

bedtools intersect -a $data_path/intervals/1/b37.bed -b margin.bed > $out_prefix.margin.bed && rm margin.bed # insure that the region within b37.bed

bedtools getfasta -fi $ref_genome -bed $out_prefix.margin.bed -fo $(basename $2).fasta

. $cmd_done